package com.monkeygroup.mygoplus.ecchat.ui.chatting.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.ecchat.common.utils.DensityUtil;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.model.TopPanelModel;

import java.util.List;

/**
 * Created by Administrator on 2017/7/3.
 */
public class ChatPanelAdapter extends BaseAdapter {
    private List<TopPanelModel> itemData;
    private Context mContext;
    private LayoutInflater inflater;

    public ChatPanelAdapter(Context context, List<TopPanelModel> data) {
        mContext = context;
        itemData = data;
        this.inflater = LayoutInflater.from(context);
    }

    private void setViewParams(TextView textView, ImageView imageView) {
        textView.setTextColor(mContext.getResources().getColor(R.color.white));
        ViewGroup.LayoutParams icon_lp = imageView.getLayoutParams();
        icon_lp.width = DensityUtil.dip2px(30);
        icon_lp.height = DensityUtil.dip2px(30);
        imageView.setLayoutParams(icon_lp);
    }

    @Override
    public int getCount() {
        if (itemData != null) {
            return itemData.size();
        }

        return 0;
    }

    @Override
    public TopPanelModel getItem(int position) {
        if (itemData != null) {
            return itemData.get(position);
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = null;
        ViewHolder mViewHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            view = inflater.inflate(R.layout.app_grid_item, parent, false);

            mViewHolder = new ViewHolder();
            mViewHolder.mCapabilityName = (TextView) view.findViewById(R.id.app_grid_item_name);
            mViewHolder.mCapabilityIcon = (ImageView) view.findViewById(R.id.app_grid_item_icon);
            setViewParams(mViewHolder.mCapabilityName, mViewHolder.mCapabilityIcon);
            mViewHolder.mCapabilityMask = (ImageView) view.findViewById(R.id.app_grid_item_icon_mask);
            view.setTag(mViewHolder);
        } else {
            view = convertView;
            mViewHolder = (ViewHolder) view.getTag();
        }

        TopPanelModel item = getItem(position);
        if (item != null) {
            mViewHolder.mCapabilityName.setText(mContext.getResources().getString(item.getTextId()));
            if (item.getImageId() > 0) {
                mViewHolder.mCapabilityIcon.setImageResource(item.getImageId());
            }
            mViewHolder.mCapabilityMask.setVisibility(View.GONE);
        }

        return view;
    }

    class ViewHolder {
        TextView mCapabilityName;
        ImageView mCapabilityIcon;
        ImageView mCapabilityMask;
    }
}